package ui;

import java.util.Iterator;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

import databaseConnect.SimulationExecutionRequestProcessor;
import datatypes.DiscoveryMechanismEnum;

public class SimulationExecutionScenarioTabbedPane extends JTabbedPane {

	private int simID = -1;
	private Vector<SimulationExecutionScenarioTabs> tabs = null;
	
	private boolean discIsGlobal = false;
	private DiscoveryMechanismEnum discoveryMechanism = DiscoveryMechanismEnum.FloodingRequest;
	
	public void init(int SimulationID){
		this.simID = SimulationID;
		Vector<String> tmp = SimulationExecutionRequestProcessor.getScenariosForSimulation(SimulationID);
		try {
			for (int i = 0; i < tmp.size() ; i++){
				this.getTabs().add(new SimulationExecutionScenarioTabs(SimulationID, (Integer.parseInt(tmp.get(i))), discoveryMechanism ));
				this.addTab("Scenario "+ tmp.get(i), this.getTabs().get(i));
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void createTab (int ScenarioID, DiscoveryMechanismEnum discovery){
		this.getTabs().add(new SimulationExecutionScenarioTabs(this.simID, ScenarioID , discovery));
		this.addTab("Scenario "+ ScenarioID, this.getTabs().lastElement());
	}
	
	public void initLocal(int SimulationID, Vector<DiscoveryMechanismEnum> discovery){
		Vector<String> scenarioIDs = SimulationExecutionRequestProcessor.getScenariosForSimulation(SimulationID);
		this.simID = SimulationID;
		try {
			for (int i = 0; i < scenarioIDs.size() ; i++){
				this.discoveryMechanism = discovery.get(i);
				this.getTabs().add(new SimulationExecutionScenarioTabs(this.simID ,(Integer.parseInt(scenarioIDs.get(i))), discoveryMechanism ));
				this.addTab("Scenario "+ scenarioIDs.get(i), this.getTabs().get(i));
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private Vector<SimulationExecutionScenarioTabs> getTabs(){
		if (tabs == null){
			tabs = new Vector<SimulationExecutionScenarioTabs>();
		}
		return tabs;
	}
	
	protected void start(int numberOfSteps){
		for (int i = 0 ; i < tabs.size(); i++){
			tabs.get(i).start(numberOfSteps);
		}
	}
	
	
	protected void stop(){
		for (int i = 0 ; i < tabs.size(); i++){
			tabs.get(i).stop();
		}
	}

	public void setGlobal(DiscoveryMechanismEnum selectedItem) {
		this.discIsGlobal = true;
		this.discoveryMechanism = selectedItem;
	}
	
	
	
}
